﻿// Decompiled with JetBrains decompiler
// Type: Commom.Util.Encrypt
// Assembly: MT5Trade, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
// MVID: 8045A2CB-DBD6-41AD-8804-F98CAC1BDC8D
// Assembly location: C:\projects\trade\trade_tool\trade0812\MT5Trade.dll

using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;

#nullable enable
namespace Commom.Util;

public static class Encrypt
{
  public static string DesKey = "#s^un2ye21fcv%|f0XpR,+vh";
  private static byte[] _iv;
  public static string AesKey = "QaP1AF8utIarcBqdhYTZpVGbiNQ9M6IL";

  public static string Md5By16(string value) => Encrypt.Md5By16(value, Encoding.UTF8);

  public static string Md5By16(string value, Encoding encoding)
  {
    return Encrypt.Md5(value, encoding, new int?(4), new int?(8));
  }

  private static string Md5(string value, Encoding encoding, int? startIndex, int? length)
  {
    if (string.IsNullOrWhiteSpace(value))
      return string.Empty;
    MD5 cryptoServiceProvider = MD5.Create();
    string str;
    try
    {
      byte[] hash = cryptoServiceProvider.ComputeHash(encoding.GetBytes(value));
      str = !startIndex.HasValue ? BitConverter.ToString(hash) : BitConverter.ToString(hash, startIndex.SafeValue<int>(), length.SafeValue<int>());
    }
    finally
    {
      cryptoServiceProvider.Clear();
    }
    return str.Replace("-", "");
  }

  public static string Md5By32(string value) => Encrypt.Md5By32(value, Encoding.UTF8);

  public static string Md5By32(string value, Encoding encoding)
  {
    return Encrypt.Md5(value, encoding, new int?(), new int?());
  }

  public static string DesEncrypt(object value) => Encrypt.DesEncrypt(value, Encrypt.DesKey);

  public static string DesEncrypt(object value, string key)
  {
    return Encrypt.DesEncrypt(value, key, Encoding.UTF8);
  }

  public static string DesEncrypt(object value, string key, Encoding encoding)
  {
    string text = value.SafeString();
    if (!Encrypt.ValidateDes(text, key))
      return string.Empty;
    using (ICryptoTransform encryptor = Encrypt.CreateDesProvider(key).CreateEncryptor())
      return Encrypt.GetEncryptResult(text, encoding, encryptor);
  }

  private static bool ValidateDes(string text, string key)
  {
    return !string.IsNullOrWhiteSpace(text) && !string.IsNullOrWhiteSpace(key) && key.Length == 24;
  }

  private static TripleDES CreateDesProvider(string key)
  {
    TripleDES desProvider = TripleDES.Create();
    desProvider.Key = Encoding.ASCII.GetBytes(key);
    desProvider.Mode = CipherMode.ECB;
    desProvider.Padding = PaddingMode.PKCS7;
    return desProvider;
  }

  private static string GetEncryptResult(
    string value,
    Encoding encoding,
    ICryptoTransform transform)
  {
    byte[] bytes = encoding.GetBytes(value);
    return Convert.ToBase64String(transform.TransformFinalBlock(bytes, 0, bytes.Length));
  }

  public static string DesDecrypt(object value) => Encrypt.DesDecrypt(value, Encrypt.DesKey);

  public static string DesDecrypt(object value, string key)
  {
    return Encrypt.DesDecrypt(value, key, Encoding.UTF8);
  }

  public static string DesDecrypt(object value, string key, Encoding encoding)
  {
    string text = value.SafeString();
    if (!Encrypt.ValidateDes(text, key))
      return string.Empty;
    using (ICryptoTransform decryptor = Encrypt.CreateDesProvider(key).CreateDecryptor())
      return Encrypt.GetDecryptResult(text, encoding, decryptor);
  }

  private static string GetDecryptResult(
    string value,
    Encoding encoding,
    ICryptoTransform transform)
  {
    byte[] inputBuffer = Convert.FromBase64String(value);
    byte[] bytes = transform.TransformFinalBlock(inputBuffer, 0, inputBuffer.Length);
    return encoding.GetString(bytes);
  }

  private static byte[] Iv
  {
    get
    {
      if (Encrypt._iv == null)
      {
        int length = 16 /*0x10*/;
        Encrypt._iv = new byte[length];
        for (int index = 0; index < length; ++index)
          Encrypt._iv[index] = (byte) 0;
      }
      return Encrypt._iv;
    }
  }

  public static string AesEncrypt(string value) => Encrypt.AesEncrypt(value, Encrypt.AesKey);

  public static string AesEncrypt(string value, string key)
  {
    return Encrypt.AesEncrypt(value, key, Encoding.UTF8);
  }

  public static string AesEncrypt(string value, string key, Encoding encoding)
  {
    if (string.IsNullOrWhiteSpace(value) || string.IsNullOrWhiteSpace(key))
      return string.Empty;
    Aes rijndaelManaged = Encrypt.CreateRijndaelManaged(key, encoding);
    using (ICryptoTransform encryptor = rijndaelManaged.CreateEncryptor(rijndaelManaged.Key, rijndaelManaged.IV))
      return Encrypt.GetEncryptResult(value, encoding, encryptor);
  }

  private static Aes CreateRijndaelManaged(
    string key,
    Encoding encoding,
    CipherMode cipherMode = CipherMode.CBC)
  {
    Aes rijndaelManaged = Aes.Create();
    rijndaelManaged.Key = encoding.GetBytes(key);
    rijndaelManaged.Mode = cipherMode;
    rijndaelManaged.Padding = PaddingMode.PKCS7;
    rijndaelManaged.IV = Encrypt.Iv;
    return rijndaelManaged;
  }

  public static string AesDecrypt(string value) => Encrypt.AesDecrypt(value, Encrypt.AesKey);

  public static string AesDecrypt(string value, string key)
  {
    return Encrypt.AesDecrypt(value, key, Encoding.UTF8);
  }

  public static string AesDecrypt(
    string value,
    string key,
    Encoding encoding,
    CipherMode cipherMode = CipherMode.CBC)
  {
    if (string.IsNullOrWhiteSpace(value) || string.IsNullOrWhiteSpace(key))
      return string.Empty;
    Aes rijndaelManaged = Encrypt.CreateRijndaelManaged(key, encoding, cipherMode);
    using (ICryptoTransform decryptor = rijndaelManaged.CreateDecryptor(rijndaelManaged.Key, rijndaelManaged.IV))
      return Encrypt.GetDecryptResult(value, encoding, decryptor);
  }

  public static string RsaSign(string value, string key)
  {
    return Encrypt.RsaSign(value, key, Encoding.UTF8);
  }

  public static string RsaSign(string value, string key, Encoding encoding)
  {
    return Encrypt.RsaSign(value, key, encoding, RSAType.RSA);
  }

  public static string Rsa2Sign(string value, string key)
  {
    return Encrypt.Rsa2Sign(value, key, Encoding.UTF8);
  }

  public static string Rsa2Sign(string value, string key, Encoding encoding)
  {
    return Encrypt.RsaSign(value, key, encoding, RSAType.RSA2);
  }

  private static string RsaSign(string value, string key, Encoding encoding, RSAType type)
  {
    return string.IsNullOrWhiteSpace(value) || string.IsNullOrWhiteSpace(key) ? string.Empty : new RsaHelper(type, encoding, key).Sign(value);
  }

  public static bool RsaVerify(string value, string publicKey, string sign)
  {
    return Encrypt.RsaVerify(value, publicKey, sign, Encoding.UTF8);
  }

  public static bool RsaVerify(string value, string publicKey, string sign, Encoding encoding)
  {
    return Encrypt.RsaVerify(value, publicKey, sign, encoding, RSAType.RSA);
  }

  public static bool Rsa2Verify(string value, string publicKey, string sign)
  {
    return Encrypt.Rsa2Verify(value, publicKey, sign, Encoding.UTF8);
  }

  public static bool Rsa2Verify(string value, string publicKey, string sign, Encoding encoding)
  {
    return Encrypt.RsaVerify(value, publicKey, sign, encoding, RSAType.RSA2);
  }

  private static bool RsaVerify(
    string value,
    string publicKey,
    string sign,
    Encoding encoding,
    RSAType type)
  {
    return !string.IsNullOrWhiteSpace(value) && !string.IsNullOrWhiteSpace(publicKey) && !string.IsNullOrWhiteSpace(sign) && new RsaHelper(type, encoding, publicKey: publicKey).Verify(value, sign);
  }

  public static string HmacSha256(string value, string key)
  {
    return Encrypt.HmacSha256(value, key, Encoding.UTF8);
  }

  public static string HmacSha256(string value, string key, Encoding encoding)
  {
    return string.IsNullOrWhiteSpace(value) || string.IsNullOrWhiteSpace(key) ? string.Empty : string.Join("", ((IEnumerable<byte>) new HMACSHA256(encoding.GetBytes(key)).ComputeHash(encoding.GetBytes(value))).ToList<byte>().Select<byte, string>((Func<byte, string>) (t => t.ToString("x2"))).ToArray<string>());
  }
}
